{% extends "lists/base.html" %}
{% load staticfiles %}
{% load crispy_forms_tags %}

{% block title %}
Edit {{ list.title }}
{% endblock %}


{% block breadcrumb %}
<li><a href="{% url "home" %}">Home</a></li>
<li><a href="{% url "lists" %}">Lists</a></li>
<li class="current">Edit {{ list.title }}</li>
{% endblock %}

{% block content %}
<div class="row">
  <div class="large-12 columns">
    {% crispy form %}
  </div>
</div>

<div id="deletemodal" class="reveal-modal">
    <h1>Are you sure ?</h1>
    <p>You are about to delete <strong><a href="#" id="todel">title</a></strong>.</p>
    <a href="#" class="large button" id="close">Take me back</a> &nbsp;
    <a href="#" class="large button alert">I'm sure.</a>
  <a class="close-reveal-modal">&#215;</a>
</div>
{% endblock %}


{% block last_body %}
<script type="text/javascript">
  var delay = (function() {
    var timer = 0;
    return function(callback, ms) {
      clearTimeout (timer);
      timer = setTimeout(callback, ms);
    };
  })();

  $('#id_url').keyup(function() {
    delay(function() {
      if($('#id_url').val().length > 0) {
        $('#div_id_mdcontent').hide();
      } else {
        $('#div_id_mdcontent').show();
      }
    }, 100);
  });
</script>
<script>

    $('a.button.alert').on('click', function(e) {
      e.preventDefault();
      var id = $(this).attr('data-id');
      var title = $('#id_title').val();
      $list = $('lala');
      var link = $list.attr('href');
      $('#deletemodal > a.alert').attr('data-id', id);
      $('#todel').text(title);
      $('#todel').attr('href', link);
      $('#deletemodal').foundation('reveal', 'open');
    });

    $('#close').on('click', function() {
      $('#deletemodal').foundation('reveal', 'close');
    });

    $('#deletemodal > a.alert').on('click', function() {
      var id = $(this).attr('data-id');
      $.ajax({
        type:"POST",
        url: "/list/delete/"+id,
        data: 'csrfmiddlewaretoken={{csrf_token}}'
      }).done(function(xhr) {
        $('#deletemodal').foundation('reveal', 'close');
        window.location.replace("/");
      });
    });
</script>
{% endblock last_body %}
